<?php

class Shop_Widgets_Advertise_Widget extends Tomato_Widget
{
    protected function _prepareShow()
    {
        $request = Zend_Controller_Front::getInstance()->getRequest();
        $lang = $request->getParam('lang');
        $count = 12;

        $category_id = $request->getParam('cat',0);

        /**
        * Get connection to database
        **/
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();

        /**
         * Category models access to data
         */
        $productDao = Tomato_Model_Dao_Factory::getInstance()->setModule('shop')->getProductDao();
        $productDao->setDbConnection($conn);
        $productDao->setLang($lang);

        /**
        * Enterprise model access to data
        **/
        $enterpriseDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getEnterpriseDao();
        $enterpriseDao->setDbConnection($conn);

        /**
         * Category models access to data
         */
        $categoryDao = Tomato_Model_Dao_Factory::getInstance()->setModule('shop')->getCategoryDao();
        $categoryDao->setDbConnection($conn);
        $categoryDao->setLang($lang);

        $products = $productDao->getAdvertise($count, $category_id);
        $this->_view->assign('products', $products);

        /**
        * Get category of product
        * Enterprise of product
        **/
        $arrPC = array();
        $arrPE = array();
        foreach($products as $product){
            $category_id = substr(substr($product->category_id,1),0,-1);
            $objCategory = $categoryDao->getById($category_id);
            $arrPC[$product->product_id] = $objCategory;

            $objEnterprise = $enterpriseDao->getById($product->enterprise_id);
            $arrPE[$product->product_id] = $objEnterprise;
        }
        $this->_view->assign('arrPC', $arrPC);
        $this->_view->assign('arrPE', $arrPE);
    }
    protected function _prepareConfig()
    {

    }
}
